package com.jy.adscm.operation.cg.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.cg.bean.*;
import com.jy.adscm.operation.cg.mapper.mysql.*;
import com.jy.adscm.operation.cg.service.MachineProcessService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.operation.mscyb.bean.*;
import com.jy.adscm.operation.mscyb.mapper.mysql.*;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.utils.AdscmDateUtils;
import com.jy.adscm.utils.HttpRequest;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-07-12
 */
@Service
public class MachineProcessServiceImpl extends ServiceImpl<MachineProcessMapper, MachineProcess> implements MachineProcessService {
    @Autowired
    JqbzSettingsMapper jqbzSettingsMapper;
    @Autowired
    JqzzSettingMapper jqzzSettingMapper;
    @Autowired
    JqcsSettingMapper jqcsSettingMapper;
    @Autowired
    JqdbSettingsMapper jqdbSettingsMapper;
    @Autowired
    JqbzSettingsInputMapper jqbzSettingsInputMapper;
    @Autowired
    JqzzSettingInputMapper jqzzSettingInputMapper;
    @Autowired
    JqdbSettingsInputMapper jqdbSettingsInputMapper;
    @Autowired
    CgBomMapper cgBomMapper;
    @Autowired
    CgMaterialMapper cgMaterialMapper;
    @Autowired
    CgBomSupplierMapper cgBomSupplierMapper;
    @Autowired
    CgSupplierMapper cgSupplierMapper;
    @Autowired
    CgPurchasePlanMapper cgPurchasePlanMapper;
    @Autowired
    JqPlanMapper jqPlanMapper;
    @Override
    public List<MachineProcess> selectMachineProcess(QueryCondition queryCondition) {
        return null;
    }

  /*  @Override
    public PlanResult selectMac(QueryCondition queryCondition) throws AdscmSystemException{
        List<MachineProcess> result =new ArrayList<>();
        //首先按照顺序查询出工序
        Wrapper<MachineProcess> wrapper=new EntityWrapper<>();
        wrapper.orderBy("rank",false);
        List<MachineProcess> machineProcesses = super.selectList(wrapper);
        DecimalFormat df=new DecimalFormat("#.00");
       //根据机型查找工序
        List<MachineProcess> from =new ArrayList<>();
        if (queryCondition.getMacType()==1){
            //zxdc,血浆单采机
            //打包
            Wrapper<JqdbSettings> jqdbSettingsWrapper=new EntityWrapper<>();
            jqdbSettingsWrapper.eq("type",1);
            jqdbSettingsWrapper.orderBy("rank",false);
            List<JqdbSettings> jqdbSettings = jqdbSettingsMapper.selectList(jqdbSettingsWrapper);
            for (JqdbSettings jqdbSetting : jqdbSettings) {
                MachineProcess newmachineProcess =new MachineProcess();
                newmachineProcess.setId(jqdbSetting.getId());
                newmachineProcess.setName(jqdbSetting.getName());
                newmachineProcess.setUseTime(jqdbSetting.getUseTime());
                newmachineProcess.setDeTime(jqdbSetting.getUseTime());
                newmachineProcess.setRank(jqdbSetting.getRank());
                Wrapper<JqdbSettingsInput> jqdbSettingsInputWrapper=new EntityWrapper<>();
                jqdbSettingsInputWrapper.eq("pid",jqdbSetting.getId());
                List<JqdbSettingsInput> jqdbSettingsInputs = jqdbSettingsInputMapper.selectList(jqdbSettingsInputWrapper);
                List<CgMaterial> cgMaterials=new ArrayList<>();
                for (JqdbSettingsInput jqdbSettingsInput : jqdbSettingsInputs) {
                    CgMaterial cgMaterial = cgMaterialMapper.selectById(jqdbSettingsInput.getCid());
                    Wrapper<CgBomSupplier> supplierWrapper=new EntityWrapper<>();
                    supplierWrapper.eq("bid",cgMaterial.getId());
                    List<CgBomSupplier> cgBomSuppliers = cgBomSupplierMapper.selectList(supplierWrapper);
                    if (cgBomSuppliers!=null && cgBomSuppliers.size()>0){
                        CgBomSupplier cgBomSupplier = cgBomSuppliers.get(0);
                            cgMaterial.setUseNums(jqdbSettingsInput.getNum());
                            cgMaterials.add(cgMaterial);
                    }

                }
                newmachineProcess.setCgMaterials(cgMaterials);
                from.add(newmachineProcess);
            }
            //出厂测试
            Wrapper<JqcsSetting> jqcsSettingWrapper=new EntityWrapper<>();
            jqcsSettingWrapper.eq("type",1);
            jqcsSettingWrapper.orderBy("rank",false);
            List<JqcsSetting> jqcsSettings = jqcsSettingMapper.selectList(jqcsSettingWrapper);
            for (JqcsSetting jqcsSetting : jqcsSettings) {
                MachineProcess newmachineProcess =new MachineProcess();
                newmachineProcess.setId(jqcsSetting.getId());
                newmachineProcess.setName(jqcsSetting.getName());
                newmachineProcess.setUseTime(jqcsSetting.getUseTime());
                newmachineProcess.setRank(jqcsSetting.getRank());
                newmachineProcess.setDeTime(jqcsSetting.getUseTime());
                from.add(newmachineProcess);
            }
            //总装查询工序
            Wrapper<JqzzSetting> jqzzSettingWrapper=new EntityWrapper<>();
            jqzzSettingWrapper.eq("type",1);
            jqzzSettingWrapper.orderBy("rank",false);
            List<JqzzSetting> jqzzSettings = jqzzSettingMapper.selectList(jqzzSettingWrapper);
            for (JqzzSetting jqzzSetting : jqzzSettings) {
                MachineProcess newmachineProcess =new MachineProcess();
                newmachineProcess.setId(jqzzSetting.getId());
                newmachineProcess.setName(jqzzSetting.getName());
                newmachineProcess.setUseTime(jqzzSetting.getUseTime()*jqzzSetting.getNums());
                newmachineProcess.setRank(jqzzSetting.getRank());
                newmachineProcess.setDeTime(jqzzSetting.getUseTime());
                //查询总装需要的物料
                Wrapper<JqzzSettingInput> jqzzSettingInputWrapper=new EntityWrapper<>();
                jqzzSettingInputWrapper.eq("pid",jqzzSetting.getId());
                List<JqzzSettingInput> jqzzSettingInputs = jqzzSettingInputMapper.selectList(jqzzSettingInputWrapper);
                List<CgMaterial> cgMaterials=new ArrayList<>();
                for (JqzzSettingInput jqzzSettingInput : jqzzSettingInputs) {
                    CgMaterial cgMaterial = cgMaterialMapper.selectById(jqzzSettingInput.getCid());
                    Wrapper<CgBomSupplier> supplierWrapper=new EntityWrapper<>();
                    supplierWrapper.eq("bid",cgMaterial.getId());
                    List<CgBomSupplier> cgBomSuppliers = cgBomSupplierMapper.selectList(supplierWrapper);
                    if (cgBomSuppliers!=null && cgBomSuppliers.size()>0){
                        CgBomSupplier cgBomSupplier = cgBomSuppliers.get(0);
                            cgMaterial.setUseNums(jqzzSettingInput.getNum()*jqzzSetting.getNums());
                            cgMaterials.add(cgMaterial);
                    }
                }
                newmachineProcess.setCgMaterials(cgMaterials);

                from.add(newmachineProcess);
            }
            //查询对应的部装工序
            Wrapper<JqbzSettings> jqbzSettingsWrapper=new EntityWrapper<>();
            jqbzSettingsWrapper.eq("type",1);
            jqbzSettingsWrapper.orderBy("rank",false);
            List<JqbzSettings> jqbzSettings = jqbzSettingsMapper.selectList(jqbzSettingsWrapper);
            for (JqbzSettings jqbzSetting : jqbzSettings) {
                MachineProcess newmachineProcess =new MachineProcess();
                newmachineProcess.setId(jqbzSetting.getId());
                newmachineProcess.setName(jqbzSetting.getName());
                newmachineProcess.setUseTime(jqbzSetting.getUseTime()*jqbzSetting.getNums());
                newmachineProcess.setRank(jqbzSetting.getRank());
                newmachineProcess.setDeTime(jqbzSetting.getUseTime());
                //查询该工序下需要多少个种零件
                Wrapper<JqbzSettingsInput> jqbzSettingsInputWrapper=new EntityWrapper<>();
                jqbzSettingsInputWrapper.eq("pid",jqbzSetting.getId());
                List<JqbzSettingsInput> jqbzSettingsInputs = jqbzSettingsInputMapper.selectList(jqbzSettingsInputWrapper);
                List<CgMaterial> cgMaterials=new ArrayList<>();
                for (JqbzSettingsInput jqbzSettingsInput : jqbzSettingsInputs) {
                    CgMaterial cgMaterial = cgMaterialMapper.selectById(jqbzSettingsInput.getCid());
                    Wrapper<CgBomSupplier> supplierWrapper=new EntityWrapper<>();
                    supplierWrapper.eq("bid",cgMaterial.getId());
                    List<CgBomSupplier> cgBomSuppliers = cgBomSupplierMapper.selectList(supplierWrapper);
                    if (cgBomSuppliers!=null && cgBomSuppliers.size()>0){
                        CgBomSupplier cgBomSupplier = cgBomSuppliers.get(0);
                            cgMaterial.setUseNums(jqbzSettingsInput.getNum()*jqbzSetting.getNums());
                            cgMaterials.add(cgMaterial);
                    }
                }
                newmachineProcess.setCgMaterials(cgMaterials);
                from.add(newmachineProcess);
            }




        }
        if (queryCondition.getMacType()==2){
            //zxdc-1 血浆单采机
            //打包
            Wrapper<JqdbSettings> jqdbSettingsWrapper=new EntityWrapper<>();
            jqdbSettingsWrapper.eq("type",2);
            jqdbSettingsWrapper.orderBy("rank",false);
            List<JqdbSettings> jqdbSettings = jqdbSettingsMapper.selectList(jqdbSettingsWrapper);
            for (JqdbSettings jqdbSetting : jqdbSettings) {
                MachineProcess newmachineProcess =new MachineProcess();
                newmachineProcess.setId(jqdbSetting.getId());
                newmachineProcess.setName(jqdbSetting.getName());
                newmachineProcess.setUseTime(jqdbSetting.getUseTime());
                newmachineProcess.setRank(jqdbSetting.getRank());
                newmachineProcess.setDeTime(jqdbSetting.getUseTime());
                Wrapper<JqdbSettingsInput> jqdbSettingsInputWrapper=new EntityWrapper<>();
                jqdbSettingsInputWrapper.eq("pid",jqdbSetting.getId());
                List<JqdbSettingsInput> jqdbSettingsInputs = jqdbSettingsInputMapper.selectList(jqdbSettingsInputWrapper);
                List<CgMaterial> cgMaterials=new ArrayList<>();
                for (JqdbSettingsInput jqdbSettingsInput : jqdbSettingsInputs) {
                    CgMaterial cgMaterial = cgMaterialMapper.selectById(jqdbSettingsInput.getCid());
                    Wrapper<CgBomSupplier> supplierWrapper=new EntityWrapper<>();
                    supplierWrapper.eq("bid",cgMaterial.getId());
                    List<CgBomSupplier> cgBomSuppliers = cgBomSupplierMapper.selectList(supplierWrapper);
                    if (cgBomSuppliers!=null && cgBomSuppliers.size()>0){
                        CgBomSupplier cgBomSupplier = cgBomSuppliers.get(0);
                            cgMaterial.setUseNums(jqdbSettingsInput.getNum());
                            cgMaterials.add(cgMaterial);
                    }
                }
                newmachineProcess.setCgMaterials(cgMaterials);
                from.add(newmachineProcess);
            }
            //出厂测试
            Wrapper<JqcsSetting> jqcsSettingWrapper=new EntityWrapper<>();
            jqcsSettingWrapper.eq("type",2);
            jqcsSettingWrapper.orderBy("rank",false);
            List<JqcsSetting> jqcsSettings = jqcsSettingMapper.selectList(jqcsSettingWrapper);
            for (JqcsSetting jqcsSetting : jqcsSettings) {
                MachineProcess newmachineProcess =new MachineProcess();
                newmachineProcess.setId(jqcsSetting.getId());
                newmachineProcess.setName(jqcsSetting.getName());
                newmachineProcess.setUseTime(jqcsSetting.getUseTime());
                newmachineProcess.setRank(jqcsSetting.getRank());
                newmachineProcess.setDeTime(jqcsSetting.getUseTime());
                from.add(newmachineProcess);
            }
            //总装查询工序
            Wrapper<JqzzSetting> jqzzSettingWrapper=new EntityWrapper<>();
            jqzzSettingWrapper.eq("type",2);
            jqzzSettingWrapper.orderBy("rank",false);
            List<JqzzSetting> jqzzSettings = jqzzSettingMapper.selectList(jqzzSettingWrapper);
            for (JqzzSetting jqzzSetting : jqzzSettings) {
                MachineProcess newmachineProcess =new MachineProcess();
                newmachineProcess.setId(jqzzSetting.getId());
                newmachineProcess.setName(jqzzSetting.getName());
                newmachineProcess.setUseTime(jqzzSetting.getUseTime()*jqzzSetting.getNums());
                newmachineProcess.setRank(jqzzSetting.getRank());
                newmachineProcess.setDeTime(jqzzSetting.getUseTime());
                //查询总装需要的物料
                Wrapper<JqzzSettingInput> jqzzSettingInputWrapper=new EntityWrapper<>();
                jqzzSettingInputWrapper.eq("pid",jqzzSetting.getId());
                List<JqzzSettingInput> jqzzSettingInputs = jqzzSettingInputMapper.selectList(jqzzSettingInputWrapper);
                List<CgMaterial> cgMaterials=new ArrayList<>();
                for (JqzzSettingInput jqzzSettingInput : jqzzSettingInputs) {
                    CgMaterial cgMaterial = cgMaterialMapper.selectById(jqzzSettingInput.getCid());
                    if (!AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial,null)){
                        continue;
                    }
                    Wrapper<CgBomSupplier> supplierWrapper=new EntityWrapper<>();
                    supplierWrapper.eq("bid",cgMaterial.getId());
                    List<CgBomSupplier> cgBomSuppliers = cgBomSupplierMapper.selectList(supplierWrapper);
                    if (cgBomSuppliers!=null && cgBomSuppliers.size()>0){
                        CgBomSupplier cgBomSupplier = cgBomSuppliers.get(0);
                            cgMaterial.setUseNums(jqzzSettingInput.getNum()*jqzzSetting.getNums());
                            cgMaterials.add(cgMaterial);
                    }
                }
                newmachineProcess.setCgMaterials(cgMaterials);

                from.add(newmachineProcess);
            }
            //部装
            Wrapper<JqbzSettings> jqbzSettingsWrapper=new EntityWrapper<>();
            jqbzSettingsWrapper.eq("type",2);
            jqbzSettingsWrapper.orderBy("rank",false);
            List<JqbzSettings> jqbzSettings = jqbzSettingsMapper.selectList(jqbzSettingsWrapper);
            for (JqbzSettings jqbzSetting : jqbzSettings) {
                MachineProcess newmachineProcess =new MachineProcess();
                newmachineProcess.setId(jqbzSetting.getId());
                newmachineProcess.setName(jqbzSetting.getName());
                newmachineProcess.setUseTime(jqbzSetting.getUseTime()*jqbzSetting.getNums());
                newmachineProcess.setRank(jqbzSetting.getRank());
                newmachineProcess.setDeTime(jqbzSetting.getUseTime());
                //查询该工序下需要多少个种零件
                Wrapper<JqbzSettingsInput> jqbzSettingsInputWrapper=new EntityWrapper<>();
                jqbzSettingsInputWrapper.eq("pid",jqbzSetting.getId());
                List<JqbzSettingsInput> jqbzSettingsInputs = jqbzSettingsInputMapper.selectList(jqbzSettingsInputWrapper);
                List<CgMaterial> cgMaterials=new ArrayList<>();
                for (JqbzSettingsInput jqbzSettingsInput : jqbzSettingsInputs) {
                    CgMaterial cgMaterial = cgMaterialMapper.selectById(jqbzSettingsInput.getCid());
                    if (!AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial,null)){
                        continue;
                    }
                    Wrapper<CgBomSupplier> supplierWrapper=new EntityWrapper<>();
                    supplierWrapper.eq("bid",cgMaterial.getId());
                    List<CgBomSupplier> cgBomSuppliers = cgBomSupplierMapper.selectList(supplierWrapper);
                    if (cgBomSuppliers!=null && cgBomSuppliers.size()>0){
                        CgBomSupplier cgBomSupplier = cgBomSuppliers.get(0);
                            cgMaterial.setUseNums(jqbzSettingsInput.getNum()*jqbzSetting.getNums());
                            cgMaterials.add(cgMaterial);
                    }
                }
                newmachineProcess.setCgMaterials(cgMaterials);
                from.add(newmachineProcess);
            }



        }

        if (queryCondition.getMacType()==3){
            //zxdc-1 血浆单采机
            //打包
            Wrapper<JqdbSettings> jqdbSettingsWrapper=new EntityWrapper<>();
            jqdbSettingsWrapper.eq("type",3);
            jqdbSettingsWrapper.orderBy("rank",true);
            List<JqdbSettings> jqdbSettings = jqdbSettingsMapper.selectList(jqdbSettingsWrapper);
            for (JqdbSettings jqdbSetting : jqdbSettings) {
                MachineProcess newmachineProcess =new MachineProcess();
                newmachineProcess.setId(jqdbSetting.getId());
                newmachineProcess.setName(jqdbSetting.getName());
                newmachineProcess.setUseTime(jqdbSetting.getUseTime());
                newmachineProcess.setRank(jqdbSetting.getRank());
                newmachineProcess.setDeTime(jqdbSetting.getUseTime());
                Wrapper<JqdbSettingsInput> jqdbSettingsInputWrapper=new EntityWrapper<>();
                jqdbSettingsInputWrapper.eq("pid",jqdbSetting.getId());
                List<JqdbSettingsInput> jqdbSettingsInputs = jqdbSettingsInputMapper.selectList(jqdbSettingsInputWrapper);
                List<CgMaterial> cgMaterials=new ArrayList<>();
                for (JqdbSettingsInput jqdbSettingsInput : jqdbSettingsInputs) {
                    CgMaterial cgMaterial = cgMaterialMapper.selectById(jqdbSettingsInput.getCid());
                    Wrapper<CgBomSupplier> supplierWrapper=new EntityWrapper<>();
                    supplierWrapper.eq("bid",cgMaterial.getId());
                    List<CgBomSupplier> cgBomSuppliers = cgBomSupplierMapper.selectList(supplierWrapper);
                    if (cgBomSuppliers!=null && cgBomSuppliers.size()>0){
                        CgBomSupplier cgBomSupplier = cgBomSuppliers.get(0);
                            cgMaterial.setUseNums(jqdbSettingsInput.getNum());
                            cgMaterials.add(cgMaterial);
                    }
                }
                newmachineProcess.setCgMaterials(cgMaterials);
                from.add(newmachineProcess);
            }
            //出厂测试
            Wrapper<JqcsSetting> jqcsSettingWrapper=new EntityWrapper<>();
            jqcsSettingWrapper.eq("type",3);
            jqcsSettingWrapper.orderBy("rank",true);
            List<JqcsSetting> jqcsSettings = jqcsSettingMapper.selectList(jqcsSettingWrapper);
            for (JqcsSetting jqcsSetting : jqcsSettings) {
                MachineProcess newmachineProcess =new MachineProcess();
                newmachineProcess.setId(jqcsSetting.getId());
                newmachineProcess.setName(jqcsSetting.getName());
                newmachineProcess.setUseTime(jqcsSetting.getUseTime());
                newmachineProcess.setDeTime(jqcsSetting.getUseTime());
                newmachineProcess.setRank(jqcsSetting.getRank());
                from.add(newmachineProcess);
            }
            //总装查询工序
            Wrapper<JqzzSetting> jqzzSettingWrapper=new EntityWrapper<>();
            jqzzSettingWrapper.eq("type",3);
            jqzzSettingWrapper.orderBy("rank",true);
            List<JqzzSetting> jqzzSettings = jqzzSettingMapper.selectList(jqzzSettingWrapper);
            for (JqzzSetting jqzzSetting : jqzzSettings) {
                MachineProcess newmachineProcess =new MachineProcess();
                newmachineProcess.setId(jqzzSetting.getId());
                newmachineProcess.setName(jqzzSetting.getName());
                newmachineProcess.setUseTime(jqzzSetting.getUseTime()*jqzzSetting.getNums());
                newmachineProcess.setRank(jqzzSetting.getRank());
                newmachineProcess.setDeTime(jqzzSetting.getUseTime());
                //查询总装需要的物料
                Wrapper<JqzzSettingInput> jqzzSettingInputWrapper=new EntityWrapper<>();
                jqzzSettingInputWrapper.eq("pid",jqzzSetting.getId());
                List<JqzzSettingInput> jqzzSettingInputs = jqzzSettingInputMapper.selectList(jqzzSettingInputWrapper);
                List<CgMaterial> cgMaterials=new ArrayList<>();
                for (JqzzSettingInput jqzzSettingInput : jqzzSettingInputs) {
                    CgMaterial cgMaterial = cgMaterialMapper.selectById(jqzzSettingInput.getCid());
                    Wrapper<CgBomSupplier> supplierWrapper=new EntityWrapper<>();
                    supplierWrapper.eq("bid",cgMaterial.getId());
                    List<CgBomSupplier> cgBomSuppliers = cgBomSupplierMapper.selectList(supplierWrapper);
                    if (cgBomSuppliers!=null && cgBomSuppliers.size()>0){
                        CgBomSupplier cgBomSupplier = cgBomSuppliers.get(0);
                            cgMaterial.setUseNums(jqzzSettingInput.getNum()*jqzzSetting.getNums());
                            cgMaterials.add(cgMaterial);
                    }
                }
                newmachineProcess.setCgMaterials(cgMaterials);

                from.add(newmachineProcess);
            }
            //部装
            Wrapper<JqbzSettings> jqbzSettingsWrapper=new EntityWrapper<>();
            jqbzSettingsWrapper.eq("type",3);
            jqbzSettingsWrapper.orderBy("rank",true);
            List<JqbzSettings> jqbzSettings = jqbzSettingsMapper.selectList(jqbzSettingsWrapper);
            for (JqbzSettings jqbzSetting : jqbzSettings) {
                MachineProcess newmachineProcess =new MachineProcess();
                newmachineProcess.setId(jqbzSetting.getId());
                newmachineProcess.setName(jqbzSetting.getName());
                newmachineProcess.setUseTime(jqbzSetting.getUseTime()*jqbzSetting.getNums());
                newmachineProcess.setRank(jqbzSetting.getRank());
                newmachineProcess.setDeTime(jqbzSetting.getUseTime());
                //查询该工序下需要多少个种零件
                Wrapper<JqbzSettingsInput> jqbzSettingsInputWrapper=new EntityWrapper<>();
                jqbzSettingsInputWrapper.eq("pid",jqbzSetting.getId());
                List<JqbzSettingsInput> jqbzSettingsInputs = jqbzSettingsInputMapper.selectList(jqbzSettingsInputWrapper);
                List<CgMaterial> cgMaterials=new ArrayList<>();
                for (JqbzSettingsInput jqbzSettingsInput : jqbzSettingsInputs) {
                    CgMaterial cgMaterial = cgMaterialMapper.selectById(jqbzSettingsInput.getCid());
                    Wrapper<CgBomSupplier> supplierWrapper=new EntityWrapper<>();
                    supplierWrapper.eq("bid",cgMaterial.getId());
                    List<CgBomSupplier> cgBomSuppliers = cgBomSupplierMapper.selectList(supplierWrapper);
                    if (cgBomSuppliers!=null && cgBomSuppliers.size()>0){
                        CgBomSupplier cgBomSupplier = cgBomSuppliers.get(0);
                            cgMaterial.setUseNums(jqbzSettingsInput.getNum()*jqbzSetting.getNums());
                            cgMaterials.add(cgMaterial);
                    }
                }
                newmachineProcess.setCgMaterials(cgMaterials);
                from.add(newmachineProcess);
            }

        }


         //使用一个map

        //完工时间
        String StartTime=  AdscmDateUtils.isWorkDate(AdscmDateUtils.getNowOnlyDateFomat1(queryCondition.getStartTime()));//转成字符串，后面还拼接


        Date temp=AdscmDateUtils.getDateFomat(StartTime+" "+"16:00:00") ;//上个工序结束时间 machineProcesses from
        for (MachineProcess machineProcess : from) {
            float  processNeedTime=   machineProcess.getUseTime()*queryCondition.getMacNums();
            Date  todaylate=  AdscmDateUtils.getDateFomat( AdscmDateUtils.getNowOnlyDateFomat1(temp)+" "+"8:00:00");
            long  remaining=(temp.getTime()-todaylate.getTime())*queryCondition.getPersons() -  (long)processNeedTime*(60*1000);

            if (remaining>0){

                machineProcess.setEndTime(temp);
                machineProcess.setStartTime(new Date(temp.getTime()-Long.valueOf(String.valueOf( Math.round(processNeedTime*(60*1000)/queryCondition.getPersons())))));
                double  a= (double) (machineProcess.getEndTime().getTime() -machineProcess.getStartTime().getTime())/(60*1000);
                machineProcess.setUseTime( Float.valueOf(df.format(a)) );
                temp=machineProcess.getStartTime();
                MachineProcess newmachineProcess =new MachineProcess();
                newmachineProcess.setId(machineProcess.getId());
                newmachineProcess.setName(machineProcess.getName());
                newmachineProcess.setStartTime(machineProcess.getStartTime());
                newmachineProcess.setEndTime(machineProcess.getEndTime());
                newmachineProcess.setUseTime(machineProcess.getUseTime());
                newmachineProcess.setRank(machineProcess.getRank());
                newmachineProcess.setCgMaterials(machineProcess.getCgMaterials());
                newmachineProcess.setDeTime(machineProcess.getDeTime());

                result.add(newmachineProcess);
            }else {

                while (remaining<0){
                    machineProcess.setEndTime(temp);
                    //获取当天的最晚时间
                    Date lateEnd = AdscmDateUtils.getDateFomat( AdscmDateUtils.getNowOnlyDateFomat1(temp)+" "+"8:00:00");
                    machineProcess.setStartTime(lateEnd);
                    double  a= (double) (machineProcess.getEndTime().getTime() -machineProcess.getStartTime().getTime())/(60*1000);
                    machineProcess.setUseTime( Float.valueOf(df.format(a)) );
                    MachineProcess newmachineProcess =new MachineProcess();
                    newmachineProcess.setId(machineProcess.getId());
                    newmachineProcess.setName(machineProcess.getName());
                    newmachineProcess.setStartTime(machineProcess.getStartTime());
                    newmachineProcess.setEndTime(machineProcess.getEndTime());
                    newmachineProcess.setUseTime(machineProcess.getUseTime());
                    newmachineProcess.setDeTime(machineProcess.getDeTime());
                    newmachineProcess.setRank(machineProcess.getRank());
                    newmachineProcess.setCgMaterials(machineProcess.getCgMaterials());
                    result.add(newmachineProcess);
                    String nextDay=  AdscmDateUtils.isWorkDate( AdscmDateUtils.getThatDateStr(machineProcess.getEndTime(),-1, Calendar.DATE));
                    System.out.println("上一天"+nextDay);
                    temp=AdscmDateUtils.getDateFomat(nextDay+" "+"16:00:00");
                    if ((8*(60*60*1000)*queryCondition.getPersons() + remaining)>0){
                        remaining=-remaining;
                    }else {
                        remaining=8*(60*60*1000)*queryCondition.getPersons() + remaining;
                    }

                }
                if (remaining==0){
                    machineProcess.setEndTime(temp);
                    Date lateEnd = AdscmDateUtils.getDateFomat( AdscmDateUtils.getNowOnlyDateFomat1(temp)+" "+"8:00:00");
                    machineProcess.setStartTime(lateEnd);
                    double  a= (double) (machineProcess.getEndTime().getTime() -machineProcess.getStartTime().getTime())/(60*1000);
                    machineProcess.setUseTime( Float.valueOf(df.format(a)) );
                    String nextDay=  AdscmDateUtils.isWorkDate(AdscmDateUtils.getThatDateStr(machineProcess.getEndTime(),-1, Calendar.DATE));
                    System.out.println("上一天"+nextDay);
                    temp=AdscmDateUtils.getDateFomat(nextDay+" "+"16:00:00");
                    MachineProcess newmachineProcess =new MachineProcess();
                    newmachineProcess.setId(machineProcess.getId());
                    newmachineProcess.setName(machineProcess.getName());
                    newmachineProcess.setStartTime(machineProcess.getStartTime());
                    newmachineProcess.setEndTime(machineProcess.getEndTime());
                    newmachineProcess.setUseTime(machineProcess.getUseTime());
                    newmachineProcess.setRank(machineProcess.getRank());
                    newmachineProcess.setCgMaterials(machineProcess.getCgMaterials());
                    newmachineProcess.setDeTime(machineProcess.getDeTime());
                    result.add(newmachineProcess);
                }
                if (remaining>0){
                    machineProcess.setEndTime(temp);
                    machineProcess.setStartTime(new Date(temp.getTime()-Long.valueOf(String.valueOf(remaining/queryCondition.getPersons()))));
                    double  a= (double) (machineProcess.getEndTime().getTime() -machineProcess.getStartTime().getTime())/(60*1000);
                    machineProcess.setUseTime( Float.valueOf(df.format(a)) );
                    temp=machineProcess.getStartTime();
                    MachineProcess newmachineProcess =new MachineProcess();
                    newmachineProcess.setId(machineProcess.getId());
                    newmachineProcess.setName(machineProcess.getName());
                    newmachineProcess.setStartTime(machineProcess.getStartTime());
                    newmachineProcess.setEndTime(machineProcess.getEndTime());
                    newmachineProcess.setUseTime(machineProcess.getUseTime());
                    newmachineProcess.setRank(machineProcess.getRank());
                    newmachineProcess.setCgMaterials(machineProcess.getCgMaterials());
                    newmachineProcess.setDeTime(machineProcess.getDeTime());
                    result.add(newmachineProcess);
                }

            }
        }
        HashMap<Integer,CgMaterial> map=new HashMap<>();
        //用于计算总金额
        Float total=0f;
        for (int i =  result.size()-1; i >=0; i--) {

            MachineProcess machineProcess = result.get(i);
            List<CgMaterial> cgMaterials = machineProcess.getCgMaterials();
            if (cgMaterials!=null &&cgMaterials.size()>0){
                List<CgMaterial> newCgMaterials=new ArrayList<>();
                //下单日期
                Date tempPreDate=null;
                //最晚支付时间
                Date tempLatePayDate=null;
                //最早支付时间
                Date tempearlyPayDate=null;

                for (CgMaterial cgMaterial : cgMaterials) {
                    CgMaterial cgMaterialFromMap = map.get(cgMaterial.getId());
                    if (!AdscmBaseUtils.getInstance().isNotEmpty(cgMaterialFromMap,null)){
                        //1.查询这个物料对应的采购商
                        Wrapper<CgBomSupplier> cgBomSupplierWrapper=new EntityWrapper<>();
                        cgBomSupplierWrapper.eq("bid",cgMaterial.getId());
                        List<CgBomSupplier> cgBomSuppliers = cgBomSupplierMapper.selectList(cgBomSupplierWrapper);
                        if (cgBomSuppliers!=null&&cgBomSuppliers.size()>0){
                            CgBomSupplier cgBomSupplier = cgBomSuppliers.get(0);
                            cgMaterial.setCgBomSupplier(cgBomSupplier);
                        }else {
                            throw new AdscmSystemException(cgMaterial.getName()+"未设置供应商，请联系采购人员设置该物料的供应商");
                        }
                        //2.到对应的机型bom中查找这个物料的用量
                        Wrapper<CgBom> cgBomWrapper=new EntityWrapper<>();
                        cgBomWrapper.eq("mid",cgMaterial.getId());
                        cgBomWrapper.eq("machine_type",queryCondition.getMacType());
                        List<CgBom> cgBoms = cgBomMapper.selectList(cgBomWrapper);
                        if (cgBoms!=null && cgBoms.size()>0){
                            CgBom cgBom = cgBoms.get(0);
                            cgMaterial.setUseNums(cgBom.getNums());
                        }
                        //3.根据剩余量计算是否要减去需求量，能否满足任务需求
                        Float remaining = cgMaterial.getRemaining();
                        Float useNums =     cgMaterial.getUseNums()*cgMaterial.getNo()*queryCondition.getMacNums();//使用量等于 单位台数*损耗系数
                        cgMaterial.setUseNums(useNums);
                        remaining=remaining-useNums;
                        if (remaining<0){
                            //需要购买
                            cgMaterial.setIsNeedBuy(1);
                            //判断数量是否小于最小采购量
                            CgBomSupplier cgBomSupplier = cgMaterial.getCgBomSupplier();
                            Integer minPurchase = cgBomSupplier.getMinPurchase();
                            remaining=-remaining;
                            if (minPurchase>remaining){
                                cgMaterial.setActNums(minPurchase.floatValue());
                                cgMaterial.setRemaining(minPurchase-remaining);
                            }else {
                                cgMaterial.setActNums(remaining);
                                cgMaterial.setRemaining(0f);
                            }
                            //采购提前期
                            Integer predateNum = cgBomSupplier.getPredate();
                            Date predate = AdscmDateUtils.getFormateDate(AdscmDateUtils.getThatDate(machineProcess.getStartTime(), -predateNum, Calendar.DATE));
                            cgMaterial.setPreDate(predate);
                            //单价
                            cgMaterial.setPrice(cgBomSupplier.getPrice());
                            //单个物料总价
                            cgMaterial.setTotals(  Float.valueOf(df.format( cgBomSupplier.getPrice()*cgMaterial.getActNums())) );
                            //总价等于单价*需求数*台数
                            total= Float.valueOf(df.format(total+cgBomSupplier.getPrice()*cgMaterial.getActNums())) ;
                            //查询供应商
                            CgSupplier cgSupplier = cgSupplierMapper.selectById(cgBomSupplier.getSid());
                            cgMaterial.setCgSupplier(cgSupplier);
                            cgMaterial.setSuppilerNums( cgSupplier.getName());
                            //设置付款方式
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgSupplier.getPaymentMethod(),null)){
                                cgMaterial.setPayMethod(cgSupplier.getPaymentMethod().toString());
                            }
                            Integer paymentPeriod = cgSupplier.getPaymentPeriod();
                            Integer paymentMethod = cgSupplier.getPaymentMethod();
                            if(!AdscmBaseUtils.getInstance().isNotEmpty(paymentMethod,null)){
                                paymentPeriod=1;
                            }else {
                                if (paymentMethod==1){//预付款
                                    if (!AdscmBaseUtils.getInstance().isNotEmpty(paymentPeriod,null)){
                                        paymentPeriod=1; //若为空付款周期为1天
                                    }
                                }
                                if (paymentMethod==2){//款到付款
                                    paymentPeriod=-predateNum;
                                }
                                if (paymentMethod==3){
                                    if (!AdscmBaseUtils.getInstance().isNotEmpty(paymentPeriod,null)){
                                        paymentPeriod=1; //若为空付款周期为1天
                                    }
                                }
                                Date latePaydate = AdscmDateUtils.getFormateDate(AdscmDateUtils.getThatDate(machineProcess.getStartTime(), paymentPeriod, Calendar.DATE));
                                cgMaterial.setLatePayDate(latePaydate);
                                if (paymentMethod==4){
                                    //根据这个到货日期算和月结号计算支付时间
                                    DecimalFormat decimalFormat=new DecimalFormat("00");
                                    String format = decimalFormat.format(paymentPeriod);
                                    String arriveDate = AdscmDateUtils.getNowOnlyDateFomat1(machineProcess.getStartTime()).substring(0,8)+format;
                                    Date date = AdscmDateUtils.parseDate(arriveDate);
                                    date= AdscmDateUtils.getThatDate(date,1,Calendar.MONTH);
                                    cgMaterial.setLatePayDate(date);
                                }
                            }

                        }else {
                            //不需要购买
                            cgMaterial.setActNums(0f);
                            cgMaterial.setRemaining(remaining);
                            cgMaterial.setIsNeedBuy(0);
                        }

                        //比较和之前的最早采购和最晚支付
                        if (!AdscmBaseUtils.getInstance().isNotEmpty(tempPreDate,null)){
                             if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial.getPreDate(),null)){
                                 tempPreDate=cgMaterial.getPreDate();
                             }
                        }else {
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial.getPreDate(),null)){
                                if (tempPreDate.getTime()>cgMaterial.getPreDate().getTime()){
                                    tempPreDate=cgMaterial.getPreDate();
                                }
                            }

                        }
                        if ( ! AdscmBaseUtils.getInstance().isNotEmpty(tempLatePayDate,null)){
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial.getLatePayDate(),null)){
                                tempLatePayDate=cgMaterial.getLatePayDate();
                            }

                        }else {
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial.getLatePayDate(),null)){
                                if (tempLatePayDate.getTime()<cgMaterial.getLatePayDate().getTime()){
                                    tempLatePayDate=cgMaterial.getLatePayDate();
                                }
                            }

                        }
                        //早付款
                        if ( ! AdscmBaseUtils.getInstance().isNotEmpty(tempearlyPayDate,null)){
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial.getLatePayDate(),null)){
                                tempearlyPayDate=cgMaterial.getLatePayDate();
                            }

                        }else {
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial.getLatePayDate(),null)){
                                if (tempearlyPayDate.getTime()>cgMaterial.getLatePayDate().getTime()){
                                    tempearlyPayDate=cgMaterial.getLatePayDate();
                                }
                            }

                        }

                        map.put(cgMaterial.getId(),cgMaterial);
                        newCgMaterials.add(cgMaterial);
                    }else {
                        if (!AdscmBaseUtils.getInstance().isNotEmpty(tempPreDate,null)){
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterialFromMap.getPreDate(),null)){
                                tempPreDate=cgMaterialFromMap.getPreDate();
                            }
                        }else {
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterialFromMap.getPreDate(),null)){
                                if (tempPreDate.getTime()>cgMaterialFromMap.getPreDate().getTime()){
                                    tempPreDate=cgMaterialFromMap.getPreDate();
                                }
                            }

                        }
                        if ( ! AdscmBaseUtils.getInstance().isNotEmpty(tempLatePayDate,null)){
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterialFromMap.getLatePayDate(),null)){
                                tempLatePayDate=cgMaterialFromMap.getLatePayDate();
                            }

                        }else {
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterialFromMap.getLatePayDate(),null)){
                                if (tempLatePayDate.getTime()<cgMaterialFromMap.getLatePayDate().getTime()){
                                    tempLatePayDate=cgMaterialFromMap.getLatePayDate();
                                }
                            }
                        }
                        //早付款
                        if ( ! AdscmBaseUtils.getInstance().isNotEmpty(tempearlyPayDate,null)){
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial.getLatePayDate(),null)){
                                tempearlyPayDate=cgMaterial.getLatePayDate();
                            }

                        }else {
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial.getLatePayDate(),null)){
                                if (tempearlyPayDate.getTime()>cgMaterial.getLatePayDate().getTime()){
                                    tempearlyPayDate=cgMaterial.getLatePayDate();
                                }
                            }

                        }
                        newCgMaterials.add(cgMaterialFromMap);
                    }
                }
                machineProcess.setCgMaterials(newCgMaterials);
                machineProcess.setPreDate(tempPreDate);
                machineProcess.setEarlyDate(tempearlyPayDate);
                machineProcess.setLateDate(tempLatePayDate);
            //    machineProcess.setTotal( total*queryCondition.getMacNums());
            }

        }


        PlanResult planResult =new PlanResult();
        planResult.setTotal(total);
        planResult.setResult(result);
        return planResult;
    }*/
    @Override
    public PlanResult selectMac(QueryCondition queryCondition) throws AdscmSystemException{
        List<MachineProcess> result =new ArrayList<>();
        //首先按照顺序查询出工序
        Wrapper<MachineProcess> wrapper=new EntityWrapper<>();
        wrapper.orderBy("rank",false);
        List<MachineProcess> machineProcesses = super.selectList(wrapper);
        DecimalFormat df=new DecimalFormat("#.00");
        //根据机型查找工序
        List<MachineProcess> from =new ArrayList<>();
        if (queryCondition.getMacType()==1){
            //zxdc,血浆单采机
            //打包
            Wrapper<JqdbSettings> jqdbSettingsWrapper=new EntityWrapper<>();
            jqdbSettingsWrapper.eq("type",1);
            jqdbSettingsWrapper.orderBy("rank",false);
            List<JqdbSettings> jqdbSettings = jqdbSettingsMapper.selectList(jqdbSettingsWrapper);
            for (JqdbSettings jqdbSetting : jqdbSettings) {
                MachineProcess newmachineProcess =new MachineProcess();
                newmachineProcess.setId(jqdbSetting.getId());
                newmachineProcess.setName(jqdbSetting.getName());
                newmachineProcess.setUseTime(jqdbSetting.getUseTime());
                newmachineProcess.setDeTime(jqdbSetting.getUseTime());
                newmachineProcess.setRank(jqdbSetting.getRank());
                Wrapper<JqdbSettingsInput> jqdbSettingsInputWrapper=new EntityWrapper<>();
                jqdbSettingsInputWrapper.eq("pid",jqdbSetting.getId());
                List<JqdbSettingsInput> jqdbSettingsInputs = jqdbSettingsInputMapper.selectList(jqdbSettingsInputWrapper);
                List<CgMaterial> cgMaterials=new ArrayList<>();
                for (JqdbSettingsInput jqdbSettingsInput : jqdbSettingsInputs) {
                    CgMaterial cgMaterial = cgMaterialMapper.selectById(jqdbSettingsInput.getCid());
                    Wrapper<CgBomSupplier> supplierWrapper=new EntityWrapper<>();
                    supplierWrapper.eq("bid",cgMaterial.getId());
                    List<CgBomSupplier> cgBomSuppliers = cgBomSupplierMapper.selectList(supplierWrapper);
                    if (cgBomSuppliers!=null && cgBomSuppliers.size()>0){
                        CgBomSupplier cgBomSupplier = cgBomSuppliers.get(0);
                        cgMaterial.setUseNums(jqdbSettingsInput.getNum());
                        cgMaterials.add(cgMaterial);
                    }

                }
                newmachineProcess.setCgMaterials(cgMaterials);
                from.add(newmachineProcess);
            }
            //出厂测试
            Wrapper<JqcsSetting> jqcsSettingWrapper=new EntityWrapper<>();
            jqcsSettingWrapper.eq("type",1);
            jqcsSettingWrapper.orderBy("rank",false);
            List<JqcsSetting> jqcsSettings = jqcsSettingMapper.selectList(jqcsSettingWrapper);
            for (JqcsSetting jqcsSetting : jqcsSettings) {
                MachineProcess newmachineProcess =new MachineProcess();
                newmachineProcess.setId(jqcsSetting.getId());
                newmachineProcess.setName(jqcsSetting.getName());
                newmachineProcess.setUseTime(jqcsSetting.getUseTime());
                newmachineProcess.setRank(jqcsSetting.getRank());
                newmachineProcess.setDeTime(jqcsSetting.getUseTime());
                from.add(newmachineProcess);
            }
            //总装查询工序
            Wrapper<JqzzSetting> jqzzSettingWrapper=new EntityWrapper<>();
            jqzzSettingWrapper.eq("type",1);
            jqzzSettingWrapper.orderBy("rank",false);
            List<JqzzSetting> jqzzSettings = jqzzSettingMapper.selectList(jqzzSettingWrapper);
            for (JqzzSetting jqzzSetting : jqzzSettings) {
                MachineProcess newmachineProcess =new MachineProcess();
                newmachineProcess.setId(jqzzSetting.getId());
                newmachineProcess.setName(jqzzSetting.getName());
                newmachineProcess.setUseTime(jqzzSetting.getUseTime()*jqzzSetting.getNums());
                newmachineProcess.setRank(jqzzSetting.getRank());
                newmachineProcess.setDeTime(jqzzSetting.getUseTime());
                //查询总装需要的物料
                Wrapper<JqzzSettingInput> jqzzSettingInputWrapper=new EntityWrapper<>();
                jqzzSettingInputWrapper.eq("pid",jqzzSetting.getId());
                List<JqzzSettingInput> jqzzSettingInputs = jqzzSettingInputMapper.selectList(jqzzSettingInputWrapper);
                List<CgMaterial> cgMaterials=new ArrayList<>();
                for (JqzzSettingInput jqzzSettingInput : jqzzSettingInputs) {
                    CgMaterial cgMaterial = cgMaterialMapper.selectById(jqzzSettingInput.getCid());
                    Wrapper<CgBomSupplier> supplierWrapper=new EntityWrapper<>();
                    supplierWrapper.eq("bid",cgMaterial.getId());
                    List<CgBomSupplier> cgBomSuppliers = cgBomSupplierMapper.selectList(supplierWrapper);
                    if (cgBomSuppliers!=null && cgBomSuppliers.size()>0){
                        CgBomSupplier cgBomSupplier = cgBomSuppliers.get(0);
                        cgMaterial.setUseNums(jqzzSettingInput.getNum()*jqzzSetting.getNums());
                        cgMaterials.add(cgMaterial);
                    }
                }
                newmachineProcess.setCgMaterials(cgMaterials);

                from.add(newmachineProcess);
            }
            //查询对应的部装工序
            Wrapper<JqbzSettings> jqbzSettingsWrapper=new EntityWrapper<>();
            jqbzSettingsWrapper.eq("type",1);
            jqbzSettingsWrapper.orderBy("rank",false);
            List<JqbzSettings> jqbzSettings = jqbzSettingsMapper.selectList(jqbzSettingsWrapper);
            for (JqbzSettings jqbzSetting : jqbzSettings) {
                MachineProcess newmachineProcess =new MachineProcess();
                newmachineProcess.setId(jqbzSetting.getId());
                newmachineProcess.setName(jqbzSetting.getName());
                newmachineProcess.setUseTime(jqbzSetting.getUseTime()*jqbzSetting.getNums());
                newmachineProcess.setRank(jqbzSetting.getRank());
                newmachineProcess.setDeTime(jqbzSetting.getUseTime());
                //查询该工序下需要多少个种零件
                Wrapper<JqbzSettingsInput> jqbzSettingsInputWrapper=new EntityWrapper<>();
                jqbzSettingsInputWrapper.eq("pid",jqbzSetting.getId());
                List<JqbzSettingsInput> jqbzSettingsInputs = jqbzSettingsInputMapper.selectList(jqbzSettingsInputWrapper);
                List<CgMaterial> cgMaterials=new ArrayList<>();
                for (JqbzSettingsInput jqbzSettingsInput : jqbzSettingsInputs) {
                    CgMaterial cgMaterial = cgMaterialMapper.selectById(jqbzSettingsInput.getCid());
                    Wrapper<CgBomSupplier> supplierWrapper=new EntityWrapper<>();
                    supplierWrapper.eq("bid",cgMaterial.getId());
                    List<CgBomSupplier> cgBomSuppliers = cgBomSupplierMapper.selectList(supplierWrapper);
                    if (cgBomSuppliers!=null && cgBomSuppliers.size()>0){
                        CgBomSupplier cgBomSupplier = cgBomSuppliers.get(0);
                        cgMaterial.setUseNums(jqbzSettingsInput.getNum()*jqbzSetting.getNums());
                        cgMaterials.add(cgMaterial);
                    }
                }
                newmachineProcess.setCgMaterials(cgMaterials);
                from.add(newmachineProcess);
            }




        }
        if (queryCondition.getMacType()==2){
            //zxdc-1 血浆单采机
            //打包
            Wrapper<JqdbSettings> jqdbSettingsWrapper=new EntityWrapper<>();
            jqdbSettingsWrapper.eq("type",2);
            jqdbSettingsWrapper.orderBy("rank",false);
            List<JqdbSettings> jqdbSettings = jqdbSettingsMapper.selectList(jqdbSettingsWrapper);
            for (JqdbSettings jqdbSetting : jqdbSettings) {
                MachineProcess newmachineProcess =new MachineProcess();
                newmachineProcess.setId(jqdbSetting.getId());
                newmachineProcess.setName(jqdbSetting.getName());
                newmachineProcess.setUseTime(jqdbSetting.getUseTime());
                newmachineProcess.setRank(jqdbSetting.getRank());
                newmachineProcess.setDeTime(jqdbSetting.getUseTime());
                Wrapper<JqdbSettingsInput> jqdbSettingsInputWrapper=new EntityWrapper<>();
                jqdbSettingsInputWrapper.eq("pid",jqdbSetting.getId());
                List<JqdbSettingsInput> jqdbSettingsInputs = jqdbSettingsInputMapper.selectList(jqdbSettingsInputWrapper);
                List<CgMaterial> cgMaterials=new ArrayList<>();
                for (JqdbSettingsInput jqdbSettingsInput : jqdbSettingsInputs) {
                    CgMaterial cgMaterial = cgMaterialMapper.selectById(jqdbSettingsInput.getCid());
                    Wrapper<CgBomSupplier> supplierWrapper=new EntityWrapper<>();
                    supplierWrapper.eq("bid",cgMaterial.getId());
                    List<CgBomSupplier> cgBomSuppliers = cgBomSupplierMapper.selectList(supplierWrapper);
                    if (cgBomSuppliers!=null && cgBomSuppliers.size()>0){
                        CgBomSupplier cgBomSupplier = cgBomSuppliers.get(0);
                        cgMaterial.setUseNums(jqdbSettingsInput.getNum());
                        cgMaterials.add(cgMaterial);
                    }
                }
                newmachineProcess.setCgMaterials(cgMaterials);
                from.add(newmachineProcess);
            }
            //出厂测试
            Wrapper<JqcsSetting> jqcsSettingWrapper=new EntityWrapper<>();
            jqcsSettingWrapper.eq("type",2);
            jqcsSettingWrapper.orderBy("rank",false);
            List<JqcsSetting> jqcsSettings = jqcsSettingMapper.selectList(jqcsSettingWrapper);
            for (JqcsSetting jqcsSetting : jqcsSettings) {
                MachineProcess newmachineProcess =new MachineProcess();
                newmachineProcess.setId(jqcsSetting.getId());
                newmachineProcess.setName(jqcsSetting.getName());
                newmachineProcess.setUseTime(jqcsSetting.getUseTime());
                newmachineProcess.setRank(jqcsSetting.getRank());
                newmachineProcess.setDeTime(jqcsSetting.getUseTime());
                from.add(newmachineProcess);
            }
            //总装查询工序
            Wrapper<JqzzSetting> jqzzSettingWrapper=new EntityWrapper<>();
            jqzzSettingWrapper.eq("type",2);
            jqzzSettingWrapper.orderBy("rank",false);
            List<JqzzSetting> jqzzSettings = jqzzSettingMapper.selectList(jqzzSettingWrapper);
            for (JqzzSetting jqzzSetting : jqzzSettings) {
                MachineProcess newmachineProcess =new MachineProcess();
                newmachineProcess.setId(jqzzSetting.getId());
                newmachineProcess.setName(jqzzSetting.getName());
                newmachineProcess.setUseTime(jqzzSetting.getUseTime()*jqzzSetting.getNums());
                newmachineProcess.setRank(jqzzSetting.getRank());
                newmachineProcess.setDeTime(jqzzSetting.getUseTime());
                //查询总装需要的物料
                Wrapper<JqzzSettingInput> jqzzSettingInputWrapper=new EntityWrapper<>();
                jqzzSettingInputWrapper.eq("pid",jqzzSetting.getId());
                List<JqzzSettingInput> jqzzSettingInputs = jqzzSettingInputMapper.selectList(jqzzSettingInputWrapper);
                List<CgMaterial> cgMaterials=new ArrayList<>();
                for (JqzzSettingInput jqzzSettingInput : jqzzSettingInputs) {
                    CgMaterial cgMaterial = cgMaterialMapper.selectById(jqzzSettingInput.getCid());
                    if (!AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial,null)){
                        continue;
                    }
                    Wrapper<CgBomSupplier> supplierWrapper=new EntityWrapper<>();
                    supplierWrapper.eq("bid",cgMaterial.getId());
                    List<CgBomSupplier> cgBomSuppliers = cgBomSupplierMapper.selectList(supplierWrapper);
                    if (cgBomSuppliers!=null && cgBomSuppliers.size()>0){
                        CgBomSupplier cgBomSupplier = cgBomSuppliers.get(0);
                        cgMaterial.setUseNums(jqzzSettingInput.getNum()*jqzzSetting.getNums());
                        cgMaterials.add(cgMaterial);
                    }
                }
                newmachineProcess.setCgMaterials(cgMaterials);

                from.add(newmachineProcess);
            }
            //部装
            Wrapper<JqbzSettings> jqbzSettingsWrapper=new EntityWrapper<>();
            jqbzSettingsWrapper.eq("type",2);
            jqbzSettingsWrapper.orderBy("rank",false);
            List<JqbzSettings> jqbzSettings = jqbzSettingsMapper.selectList(jqbzSettingsWrapper);
            for (JqbzSettings jqbzSetting : jqbzSettings) {
                MachineProcess newmachineProcess =new MachineProcess();
                newmachineProcess.setId(jqbzSetting.getId());
                newmachineProcess.setName(jqbzSetting.getName());
                newmachineProcess.setUseTime(jqbzSetting.getUseTime()*jqbzSetting.getNums());
                newmachineProcess.setRank(jqbzSetting.getRank());
                newmachineProcess.setDeTime(jqbzSetting.getUseTime());
                //查询该工序下需要多少个种零件
                Wrapper<JqbzSettingsInput> jqbzSettingsInputWrapper=new EntityWrapper<>();
                jqbzSettingsInputWrapper.eq("pid",jqbzSetting.getId());
                List<JqbzSettingsInput> jqbzSettingsInputs = jqbzSettingsInputMapper.selectList(jqbzSettingsInputWrapper);
                List<CgMaterial> cgMaterials=new ArrayList<>();
                for (JqbzSettingsInput jqbzSettingsInput : jqbzSettingsInputs) {
                    CgMaterial cgMaterial = cgMaterialMapper.selectById(jqbzSettingsInput.getCid());
                    if (!AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial,null)){
                        continue;
                    }
                    Wrapper<CgBomSupplier> supplierWrapper=new EntityWrapper<>();
                    supplierWrapper.eq("bid",cgMaterial.getId());
                    List<CgBomSupplier> cgBomSuppliers = cgBomSupplierMapper.selectList(supplierWrapper);
                    if (cgBomSuppliers!=null && cgBomSuppliers.size()>0){
                        CgBomSupplier cgBomSupplier = cgBomSuppliers.get(0);
                        cgMaterial.setUseNums(jqbzSettingsInput.getNum()*jqbzSetting.getNums());
                        cgMaterials.add(cgMaterial);
                    }
                }
                newmachineProcess.setCgMaterials(cgMaterials);
                from.add(newmachineProcess);
            }



        }

        if (queryCondition.getMacType()==3){
            //zxdc-1 血浆单采机
            //打包
            Wrapper<JqdbSettings> jqdbSettingsWrapper=new EntityWrapper<>();
            jqdbSettingsWrapper.eq("type",3);
            jqdbSettingsWrapper.orderBy("rank",true);
            List<JqdbSettings> jqdbSettings = jqdbSettingsMapper.selectList(jqdbSettingsWrapper);
            for (JqdbSettings jqdbSetting : jqdbSettings) {
                MachineProcess newmachineProcess =new MachineProcess();
                newmachineProcess.setId(jqdbSetting.getId());
                newmachineProcess.setName(jqdbSetting.getName());
                newmachineProcess.setUseTime(jqdbSetting.getUseTime());
                newmachineProcess.setRank(jqdbSetting.getRank());
                newmachineProcess.setDeTime(jqdbSetting.getUseTime());
                Wrapper<JqdbSettingsInput> jqdbSettingsInputWrapper=new EntityWrapper<>();
                jqdbSettingsInputWrapper.eq("pid",jqdbSetting.getId());
                List<JqdbSettingsInput> jqdbSettingsInputs = jqdbSettingsInputMapper.selectList(jqdbSettingsInputWrapper);
                List<CgMaterial> cgMaterials=new ArrayList<>();
                for (JqdbSettingsInput jqdbSettingsInput : jqdbSettingsInputs) {
                    CgMaterial cgMaterial = cgMaterialMapper.selectById(jqdbSettingsInput.getCid());
                    Wrapper<CgBomSupplier> supplierWrapper=new EntityWrapper<>();
                    supplierWrapper.eq("bid",cgMaterial.getId());
                    List<CgBomSupplier> cgBomSuppliers = cgBomSupplierMapper.selectList(supplierWrapper);
                    if (cgBomSuppliers!=null && cgBomSuppliers.size()>0){
                        CgBomSupplier cgBomSupplier = cgBomSuppliers.get(0);
                        cgMaterial.setUseNums(jqdbSettingsInput.getNum());
                        cgMaterials.add(cgMaterial);
                    }
                }
                newmachineProcess.setCgMaterials(cgMaterials);
                from.add(newmachineProcess);
            }
            //出厂测试
            Wrapper<JqcsSetting> jqcsSettingWrapper=new EntityWrapper<>();
            jqcsSettingWrapper.eq("type",3);
            jqcsSettingWrapper.orderBy("rank",true);
            List<JqcsSetting> jqcsSettings = jqcsSettingMapper.selectList(jqcsSettingWrapper);
            for (JqcsSetting jqcsSetting : jqcsSettings) {
                MachineProcess newmachineProcess =new MachineProcess();
                newmachineProcess.setId(jqcsSetting.getId());
                newmachineProcess.setName(jqcsSetting.getName());
                newmachineProcess.setUseTime(jqcsSetting.getUseTime());
                newmachineProcess.setDeTime(jqcsSetting.getUseTime());
                newmachineProcess.setRank(jqcsSetting.getRank());
                from.add(newmachineProcess);
            }
            //总装查询工序
            Wrapper<JqzzSetting> jqzzSettingWrapper=new EntityWrapper<>();
            jqzzSettingWrapper.eq("type",3);
            jqzzSettingWrapper.orderBy("rank",true);
            List<JqzzSetting> jqzzSettings = jqzzSettingMapper.selectList(jqzzSettingWrapper);
            for (JqzzSetting jqzzSetting : jqzzSettings) {
                MachineProcess newmachineProcess =new MachineProcess();
                newmachineProcess.setId(jqzzSetting.getId());
                newmachineProcess.setName(jqzzSetting.getName());
                newmachineProcess.setUseTime(jqzzSetting.getUseTime()*jqzzSetting.getNums());
                newmachineProcess.setRank(jqzzSetting.getRank());
                newmachineProcess.setDeTime(jqzzSetting.getUseTime());
                //查询总装需要的物料
                Wrapper<JqzzSettingInput> jqzzSettingInputWrapper=new EntityWrapper<>();
                jqzzSettingInputWrapper.eq("pid",jqzzSetting.getId());
                List<JqzzSettingInput> jqzzSettingInputs = jqzzSettingInputMapper.selectList(jqzzSettingInputWrapper);
                List<CgMaterial> cgMaterials=new ArrayList<>();
                for (JqzzSettingInput jqzzSettingInput : jqzzSettingInputs) {
                    CgMaterial cgMaterial = cgMaterialMapper.selectById(jqzzSettingInput.getCid());
                    Wrapper<CgBomSupplier> supplierWrapper=new EntityWrapper<>();
                    supplierWrapper.eq("bid",cgMaterial.getId());
                    List<CgBomSupplier> cgBomSuppliers = cgBomSupplierMapper.selectList(supplierWrapper);
                    if (cgBomSuppliers!=null && cgBomSuppliers.size()>0){
                        CgBomSupplier cgBomSupplier = cgBomSuppliers.get(0);
                        cgMaterial.setUseNums(jqzzSettingInput.getNum()*jqzzSetting.getNums());
                        cgMaterials.add(cgMaterial);
                    }
                }
                newmachineProcess.setCgMaterials(cgMaterials);

                from.add(newmachineProcess);
            }
            //部装
            Wrapper<JqbzSettings> jqbzSettingsWrapper=new EntityWrapper<>();
            jqbzSettingsWrapper.eq("type",3);
            jqbzSettingsWrapper.orderBy("rank",true);
            List<JqbzSettings> jqbzSettings = jqbzSettingsMapper.selectList(jqbzSettingsWrapper);
            for (JqbzSettings jqbzSetting : jqbzSettings) {
                MachineProcess newmachineProcess =new MachineProcess();
                newmachineProcess.setId(jqbzSetting.getId());
                newmachineProcess.setName(jqbzSetting.getName());
                newmachineProcess.setUseTime(jqbzSetting.getUseTime()*jqbzSetting.getNums());
                newmachineProcess.setRank(jqbzSetting.getRank());
                newmachineProcess.setDeTime(jqbzSetting.getUseTime());
                //查询该工序下需要多少个种零件
                Wrapper<JqbzSettingsInput> jqbzSettingsInputWrapper=new EntityWrapper<>();
                jqbzSettingsInputWrapper.eq("pid",jqbzSetting.getId());
                List<JqbzSettingsInput> jqbzSettingsInputs = jqbzSettingsInputMapper.selectList(jqbzSettingsInputWrapper);
                List<CgMaterial> cgMaterials=new ArrayList<>();
                for (JqbzSettingsInput jqbzSettingsInput : jqbzSettingsInputs) {
                    CgMaterial cgMaterial = cgMaterialMapper.selectById(jqbzSettingsInput.getCid());
                    Wrapper<CgBomSupplier> supplierWrapper=new EntityWrapper<>();
                    supplierWrapper.eq("bid",cgMaterial.getId());
                    List<CgBomSupplier> cgBomSuppliers = cgBomSupplierMapper.selectList(supplierWrapper);
                    if (cgBomSuppliers!=null && cgBomSuppliers.size()>0){
                        CgBomSupplier cgBomSupplier = cgBomSuppliers.get(0);
                        cgMaterial.setUseNums(jqbzSettingsInput.getNum()*jqbzSetting.getNums());
                        cgMaterials.add(cgMaterial);
                    }
                }
                newmachineProcess.setCgMaterials(cgMaterials);
                from.add(newmachineProcess);
            }

        }


        //使用一个map

        //完工时间
        String StartTime=  AdscmDateUtils.isWorkDate(AdscmDateUtils.getNowOnlyDateFomat1(queryCondition.getStartTime()));//转成字符串，后面还拼接


        Date temp=AdscmDateUtils.getDateFomat(StartTime+" "+"16:00:00") ;//上个工序结束时间 machineProcesses from
        for (MachineProcess machineProcess : from) {
             if ("出厂检验".equals(machineProcess.getName())){
                 machineProcess.setEndTime(temp);
                 machineProcess.setStartTime(AdscmDateUtils.getThatDate(temp,-4,Calendar.DATE));
                 temp=machineProcess.getStartTime();
                 double  a= (double) (machineProcess.getEndTime().getTime() -machineProcess.getStartTime().getTime())/(60*1000);
                 machineProcess.setUseTime( Float.valueOf(df.format(a)) );
                 MachineProcess newmachineProcess =new MachineProcess();
                 newmachineProcess.setId(machineProcess.getId());
                 newmachineProcess.setName(machineProcess.getName());
                 newmachineProcess.setStartTime(machineProcess.getStartTime());
                 newmachineProcess.setEndTime(machineProcess.getEndTime());
                 newmachineProcess.setUseTime(machineProcess.getUseTime());
                 newmachineProcess.setRank(machineProcess.getRank());
                 newmachineProcess.setCgMaterials(machineProcess.getCgMaterials());
                 newmachineProcess.setDeTime(Float.valueOf(String.valueOf(a)));
                 result.add(newmachineProcess);
             }else {
                 float  processNeedTime=   machineProcess.getUseTime()*queryCondition.getMacNums();
                 Date  todaylate=  AdscmDateUtils.getDateFomat( AdscmDateUtils.getNowOnlyDateFomat1(temp)+" "+"8:00:00");
                 long  remaining=(temp.getTime()-todaylate.getTime())*queryCondition.getPersons() -  (long)processNeedTime*(60*1000);

                 if (remaining>0){

                     machineProcess.setEndTime(temp);
                     machineProcess.setStartTime(new Date(temp.getTime()-Long.valueOf(String.valueOf( Math.round(processNeedTime*(60*1000)/queryCondition.getPersons())))));
                     double  a= (double) (machineProcess.getEndTime().getTime() -machineProcess.getStartTime().getTime())/(60*1000);
                     machineProcess.setUseTime( Float.valueOf(df.format(a)) );
                     temp=machineProcess.getStartTime();
                     MachineProcess newmachineProcess =new MachineProcess();
                     newmachineProcess.setId(machineProcess.getId());
                     newmachineProcess.setName(machineProcess.getName());
                     newmachineProcess.setStartTime(machineProcess.getStartTime());
                     newmachineProcess.setEndTime(machineProcess.getEndTime());
                     newmachineProcess.setUseTime(machineProcess.getUseTime());
                     newmachineProcess.setRank(machineProcess.getRank());
                     newmachineProcess.setCgMaterials(machineProcess.getCgMaterials());
                     newmachineProcess.setDeTime(machineProcess.getDeTime());

                     result.add(newmachineProcess);
                 }else {
                     while (remaining<0){
                         machineProcess.setEndTime(temp);
                         //获取当天的最晚时间
                         Date lateEnd = AdscmDateUtils.getDateFomat( AdscmDateUtils.getNowOnlyDateFomat1(temp)+" "+"8:00:00");
                         machineProcess.setStartTime(lateEnd);
                         double  a= (double) (machineProcess.getEndTime().getTime() -machineProcess.getStartTime().getTime())/(60*1000);
                         machineProcess.setUseTime( Float.valueOf(df.format(a)) );
                         MachineProcess newmachineProcess =new MachineProcess();
                         newmachineProcess.setId(machineProcess.getId());
                         newmachineProcess.setName(machineProcess.getName());
                         newmachineProcess.setStartTime(machineProcess.getStartTime());
                         newmachineProcess.setEndTime(machineProcess.getEndTime());
                         newmachineProcess.setUseTime(machineProcess.getUseTime());
                         newmachineProcess.setDeTime(machineProcess.getDeTime());
                         newmachineProcess.setRank(machineProcess.getRank());
                         newmachineProcess.setCgMaterials(machineProcess.getCgMaterials());
                         result.add(newmachineProcess);
                         String nextDay=  AdscmDateUtils.isWorkDate( AdscmDateUtils.getThatDateStr(machineProcess.getEndTime(),-1, Calendar.DATE));
                         System.out.println("上一天"+nextDay);
                         temp=AdscmDateUtils.getDateFomat(nextDay+" "+"16:00:00");
                         if ((8*(60*60*1000)*queryCondition.getPersons() + remaining)>0){
                             remaining=-remaining;
                         }else {
                             remaining=8*(60*60*1000)*queryCondition.getPersons() + remaining;
                         }

                     }
                     if (remaining==0){
                         machineProcess.setEndTime(temp);
                         Date lateEnd = AdscmDateUtils.getDateFomat( AdscmDateUtils.getNowOnlyDateFomat1(temp)+" "+"8:00:00");
                         machineProcess.setStartTime(lateEnd);
                         double  a= (double) (machineProcess.getEndTime().getTime() -machineProcess.getStartTime().getTime())/(60*1000);
                         machineProcess.setUseTime( Float.valueOf(df.format(a)) );
                         String nextDay=  AdscmDateUtils.isWorkDate(AdscmDateUtils.getThatDateStr(machineProcess.getEndTime(),-1, Calendar.DATE));
                         System.out.println("上一天"+nextDay);
                         temp=AdscmDateUtils.getDateFomat(nextDay+" "+"16:00:00");
                         MachineProcess newmachineProcess =new MachineProcess();
                         newmachineProcess.setId(machineProcess.getId());
                         newmachineProcess.setName(machineProcess.getName());
                         newmachineProcess.setStartTime(machineProcess.getStartTime());
                         newmachineProcess.setEndTime(machineProcess.getEndTime());
                         newmachineProcess.setUseTime(machineProcess.getUseTime());
                         newmachineProcess.setRank(machineProcess.getRank());
                         newmachineProcess.setCgMaterials(machineProcess.getCgMaterials());
                         newmachineProcess.setDeTime(machineProcess.getDeTime());
                         result.add(newmachineProcess);
                     }
                     if (remaining>0){
                         machineProcess.setEndTime(temp);
                         machineProcess.setStartTime(new Date(temp.getTime()-Long.valueOf(String.valueOf(remaining/queryCondition.getPersons()))));
                         double  a= (double) (machineProcess.getEndTime().getTime() -machineProcess.getStartTime().getTime())/(60*1000);
                         machineProcess.setUseTime( Float.valueOf(df.format(a)) );
                         temp=machineProcess.getStartTime();
                         MachineProcess newmachineProcess =new MachineProcess();
                         newmachineProcess.setId(machineProcess.getId());
                         newmachineProcess.setName(machineProcess.getName());
                         newmachineProcess.setStartTime(machineProcess.getStartTime());
                         newmachineProcess.setEndTime(machineProcess.getEndTime());
                         newmachineProcess.setUseTime(machineProcess.getUseTime());
                         newmachineProcess.setRank(machineProcess.getRank());
                         newmachineProcess.setCgMaterials(machineProcess.getCgMaterials());
                         newmachineProcess.setDeTime(machineProcess.getDeTime());
                         result.add(newmachineProcess);
                     }
                 }
             }


        }
        HashMap<Integer,CgMaterial> map=new HashMap<>();
        //用于计算总金额
        Float total=0f;
        MachineProcess machineProcess1 = result.get(result.size() - 1);
        Date startTime =  AdscmDateUtils.getThatDate(machineProcess1.getStartTime(),-1,Calendar.DATE);
        for (int i =  result.size()-1; i >=0; i--) {

            MachineProcess machineProcess = result.get(i);
            machineProcess.setArriveDate(startTime);
            List<CgMaterial> cgMaterials = machineProcess.getCgMaterials();
            if (cgMaterials!=null &&cgMaterials.size()>0){
                List<CgMaterial> newCgMaterials=new ArrayList<>();
                //下单日期
                Date tempPreDate=null;
                //最晚支付时间
                Date tempLatePayDate=null;
                //最早支付时间
                Date tempearlyPayDate=null;

                for (CgMaterial cgMaterial : cgMaterials) {
                    CgMaterial cgMaterialFromMap = map.get(cgMaterial.getId());
                    if (!AdscmBaseUtils.getInstance().isNotEmpty(cgMaterialFromMap,null)){
                        //1.查询这个物料对应的采购商
                        Wrapper<CgBomSupplier> cgBomSupplierWrapper=new EntityWrapper<>();
                        cgBomSupplierWrapper.eq("bid",cgMaterial.getId());
                        List<CgBomSupplier> cgBomSuppliers = cgBomSupplierMapper.selectList(cgBomSupplierWrapper);
                        if (cgBomSuppliers!=null&&cgBomSuppliers.size()>0){
                            CgBomSupplier cgBomSupplier = cgBomSuppliers.get(0);
                            cgMaterial.setCgBomSupplier(cgBomSupplier);
                        }else {
                            throw new AdscmSystemException(cgMaterial.getName()+"未设置供应商，请联系采购人员设置该物料的供应商");
                        }
                        //2.到对应的机型bom中查找这个物料的用量
                        Wrapper<CgBom> cgBomWrapper=new EntityWrapper<>();
                        cgBomWrapper.eq("mid",cgMaterial.getId());
                        cgBomWrapper.eq("machine_type",queryCondition.getMacType());
                        List<CgBom> cgBoms = cgBomMapper.selectList(cgBomWrapper);
                        if (cgBoms!=null && cgBoms.size()>0){
                            CgBom cgBom = cgBoms.get(0);
                            cgMaterial.setUseNums(cgBom.getNums());
                        }
                        //3.根据剩余量计算是否要减去需求量，能否满足任务需求
                        Float remaining = cgMaterial.getRemaining();
                        Float useNums =     cgMaterial.getUseNums()*cgMaterial.getNo()*queryCondition.getMacNums();//使用量等于 单位台数*损耗系数
                        cgMaterial.setUseNums(useNums);
                        remaining=remaining-useNums;
                        if (remaining<0){
                            //需要购买
                            cgMaterial.setIsNeedBuy(1);
                            //判断数量是否小于最小采购量
                            CgBomSupplier cgBomSupplier = cgMaterial.getCgBomSupplier();
                            Integer minPurchase = cgBomSupplier.getMinPurchase();
                            remaining=-remaining;
                            if (minPurchase>remaining){
                                cgMaterial.setActNums( Float.valueOf( df.format( minPurchase)));
                                cgMaterial.setRemaining(minPurchase-remaining);
                            }else {
                                cgMaterial.setActNums(Float.valueOf(df.format( remaining)));
                                cgMaterial.setRemaining(0f);
                            }
                            //采购提前期
                            Integer predateNum = cgBomSupplier.getPredate();
                            //Date predate = AdscmDateUtils.getFormateDate(AdscmDateUtils.getThatDate(machineProcess.getStartTime(), -predateNum, Calendar.DATE));
                            Date predate = AdscmDateUtils.getFormateDate(AdscmDateUtils.getThatDate(startTime, -predateNum, Calendar.DATE));
                            cgMaterial.setPreDate(predate);
                            //单价
                            cgMaterial.setPrice(cgBomSupplier.getPrice());
                            //单个物料总价
                            cgMaterial.setTotals(  Float.valueOf(df.format( cgBomSupplier.getPrice()*cgMaterial.getActNums())) );
                            //总价等于单价*需求数*台数
                            total= Float.valueOf(df.format(total+cgBomSupplier.getPrice()*cgMaterial.getActNums())) ;
                            //查询供应商
                            CgSupplier cgSupplier = cgSupplierMapper.selectById(cgBomSupplier.getSid());
                            cgMaterial.setCgSupplier(cgSupplier);
                            cgMaterial.setSuppilerNums( cgSupplier.getName());
                            //设置付款方式
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgSupplier.getPaymentMethod(),null)){
                                cgMaterial.setPayMethod(cgSupplier.getPaymentMethod().toString());
                            }
                            Integer paymentPeriod = cgSupplier.getPaymentPeriod();
                            Integer paymentMethod = cgSupplier.getPaymentMethod();
                            if(!AdscmBaseUtils.getInstance().isNotEmpty(paymentMethod,null)){
                                paymentPeriod=1;
                            }else {
                                if (paymentMethod==1){//预付款
                                    if (!AdscmBaseUtils.getInstance().isNotEmpty(paymentPeriod,null)){
                                        paymentPeriod=1; //若为空付款周期为1天
                                    }
                                }
                                if (paymentMethod==2){//款到付款
                                    paymentPeriod=-predateNum;
                                }
                                if (paymentMethod==3){
                                    if (!AdscmBaseUtils.getInstance().isNotEmpty(paymentPeriod,null)){
                                        paymentPeriod=1; //若为空付款周期为1天
                                    }
                                }
                               // Date latePaydate = AdscmDateUtils.getFormateDate(AdscmDateUtils.getThatDate(machineProcess.getStartTime(), paymentPeriod, Calendar.DATE));
                                Date latePaydate = AdscmDateUtils.getFormateDate(AdscmDateUtils.getThatDate(startTime, paymentPeriod, Calendar.DATE));
                                cgMaterial.setLatePayDate(latePaydate);
                                if (paymentMethod==4){
                                    //根据这个到货日期算和月结号计算支付时间
                                    DecimalFormat decimalFormat=new DecimalFormat("00");
                                    String format = decimalFormat.format(paymentPeriod);
                                    String arriveDate = AdscmDateUtils.getNowOnlyDateFomat1(startTime).substring(0,8)+format;
                                    Date date = AdscmDateUtils.parseDate(arriveDate);
                                    date= AdscmDateUtils.getThatDate(date,1,Calendar.MONTH);
                                    cgMaterial.setLatePayDate(date);
                                }
                            }

                        }else {
                            //不需要购买
                            cgMaterial.setActNums(0f);
                            cgMaterial.setRemaining(remaining);
                            cgMaterial.setIsNeedBuy(0);
                        }

                        //比较和之前的最早采购和最晚支付
                        if (!AdscmBaseUtils.getInstance().isNotEmpty(tempPreDate,null)){
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial.getPreDate(),null)){
                                tempPreDate=cgMaterial.getPreDate();
                            }
                        }else {
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial.getPreDate(),null)){
                                if (tempPreDate.getTime()>cgMaterial.getPreDate().getTime()){
                                    tempPreDate=cgMaterial.getPreDate();
                                }
                            }

                        }
                        if ( ! AdscmBaseUtils.getInstance().isNotEmpty(tempLatePayDate,null)){
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial.getLatePayDate(),null)){
                                tempLatePayDate=cgMaterial.getLatePayDate();
                            }

                        }else {
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial.getLatePayDate(),null)){
                                if (tempLatePayDate.getTime()<cgMaterial.getLatePayDate().getTime()){
                                    tempLatePayDate=cgMaterial.getLatePayDate();
                                }
                            }

                        }
                        //早付款
                        if ( ! AdscmBaseUtils.getInstance().isNotEmpty(tempearlyPayDate,null)){
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial.getLatePayDate(),null)){
                                tempearlyPayDate=cgMaterial.getLatePayDate();
                            }

                        }else {
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial.getLatePayDate(),null)){
                                if (tempearlyPayDate.getTime()>cgMaterial.getLatePayDate().getTime()){
                                    tempearlyPayDate=cgMaterial.getLatePayDate();
                                }
                            }

                        }

                        map.put(cgMaterial.getId(),cgMaterial);
                        newCgMaterials.add(cgMaterial);
                    }else {
                        if (!AdscmBaseUtils.getInstance().isNotEmpty(tempPreDate,null)){
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterialFromMap.getPreDate(),null)){
                                tempPreDate=cgMaterialFromMap.getPreDate();
                            }
                        }else {
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterialFromMap.getPreDate(),null)){
                                if (tempPreDate.getTime()>cgMaterialFromMap.getPreDate().getTime()){
                                    tempPreDate=cgMaterialFromMap.getPreDate();
                                }
                            }

                        }
                        if ( ! AdscmBaseUtils.getInstance().isNotEmpty(tempLatePayDate,null)){
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterialFromMap.getLatePayDate(),null)){
                                tempLatePayDate=cgMaterialFromMap.getLatePayDate();
                            }

                        }else {
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterialFromMap.getLatePayDate(),null)){
                                if (tempLatePayDate.getTime()<cgMaterialFromMap.getLatePayDate().getTime()){
                                    tempLatePayDate=cgMaterialFromMap.getLatePayDate();
                                }
                            }
                        }
                        //早付款
                        if ( ! AdscmBaseUtils.getInstance().isNotEmpty(tempearlyPayDate,null)){
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial.getLatePayDate(),null)){
                                tempearlyPayDate=cgMaterial.getLatePayDate();
                            }

                        }else {
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial.getLatePayDate(),null)){
                                if (tempearlyPayDate.getTime()>cgMaterial.getLatePayDate().getTime()){
                                    tempearlyPayDate=cgMaterial.getLatePayDate();
                                }
                            }

                        }
                        newCgMaterials.add(cgMaterialFromMap);
                    }
                }
                machineProcess.setCgMaterials(newCgMaterials);
                machineProcess.setPreDate(tempPreDate);
                machineProcess.setEarlyDate(tempearlyPayDate);
                machineProcess.setLateDate(tempLatePayDate);
                //    machineProcess.setTotal( total*queryCondition.getMacNums());
            }

        }

        PlanResult planResult =new PlanResult();
        planResult.setTotal(total);
        planResult.setResult(result);
        return planResult;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer createCgPlan(QueryCondition queryCondition, Token token) throws AdscmSystemException {
        DecimalFormat df=new DecimalFormat("#.00");
        PlanResult planResult = selectMac(queryCondition);
        List<MachineProcess> machineProcesses = planResult.getResult();
        //查询目前最大的令号
        Wrapper<CgPurchasePlan> wrapper =new EntityWrapper<>();
       // wrapper.eq("type",queryCondition.getCgType());
        //找到最大的令号
        wrapper.orderBy("make_no",false);
        wrapper.last("limit 1 ");
        List<CgPurchasePlan> cgPurchasePlans = cgPurchasePlanMapper.selectList(wrapper);
        Integer makeNo=1;
        if (cgPurchasePlans!=null && cgPurchasePlans.size()>0){
            CgPurchasePlan cgPurchasePlan = cgPurchasePlans.get(0);
            makeNo  = cgPurchasePlan.getMakeNo()+1;
        }
        HashMap<Integer,CgMaterial> map=new HashMap<>();
        for (MachineProcess machineProcess : machineProcesses) {
            List<CgMaterial> cgMaterials = machineProcess.getCgMaterials();
            if (cgMaterials!=null&&cgMaterials.size()>0){
                for (CgMaterial cgMaterial : cgMaterials) {
                    //先到map中获取cgbom 如能有就不新增
                    CgMaterial cgMaterialfromMap = map.get(cgMaterial.getId());
                    if (!AdscmBaseUtils.getInstance().isNotEmpty(cgMaterialfromMap,null)){
                        if (cgMaterial.getIsNeedBuy()==1){
                            //为空才把数据添加进去
                            CgPurchasePlan cgPurchasePlan =new CgPurchasePlan();
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial.getCgSupplier(),null)){
                                if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial.getCgSupplier().getId(),null)){
                                    cgPurchasePlan.setSid(cgMaterial.getCgSupplier().getId());
                                }
                            }
                            cgPurchasePlan.setBid(cgMaterial.getId().longValue());
                            cgPurchasePlan.setNeedNums( cgMaterial.getUseNums());

                            cgPurchasePlan.setActNums(cgMaterial.getActNums());
                            cgPurchasePlan.setPlanArriveDate( AdscmDateUtils.getFormateDate(machineProcess.getArriveDate()) );
                            cgPurchasePlan.setPrice(cgMaterial.getPrice());
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial.getPrice(),null)){
                                if (AdscmBaseUtils.getInstance().isNotEmpty(cgPurchasePlan.getActNums(),null)){
                                    cgPurchasePlan.setTotal(df.format(cgMaterial.getPrice()*cgPurchasePlan.getActNums()));
                                }
                            }

                            cgPurchasePlan.setOrderDate(cgMaterial.getPreDate());
                            if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial.getCgBomSupplier(),null)){
                                cgPurchasePlan.setMinPurchase(cgMaterial.getCgBomSupplier().getMinPurchase().toString());
                            }
                            cgPurchasePlan.setPayDate(cgMaterial.getLatePayDate());
                            cgPurchasePlan.setMacType(queryCondition.getMacType());
                            cgPurchasePlan.setType(queryCondition.getCgType());
                            cgPurchasePlan.setCreateTime(new Date());
                            cgPurchasePlan.setState(0);
                            cgPurchasePlan.setMakeNo(makeNo);
                            if (AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
                                cgPurchasePlan.setCreator(token.getUser().getId().toString());
                            }
                            //根据供应商与bom设置采购类型
                            cgPurchasePlan.setType(cgMaterial.getCgBomSupplier().getType());

                            cgPurchasePlanMapper.insert(cgPurchasePlan);
                        }
                        //修改剩余量
                        cgMaterialMapper.updateById(cgMaterial);
                        map.put(cgMaterial.getId(),cgMaterial);
                    }

                }
            }
        }
        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString(machineProcesses);
        } catch (JsonProcessingException e1) {
            e1.printStackTrace();
        }
        MachineProcess  machineProcess=new MachineProcess();
        machineProcess.setText(json);
        machineProcess.setMakeNo(queryCondition.getMakeNo());
        super.insert(machineProcess);
         //insetJqPlan(machineProcesses,queryCondition,token);
        return makeNo;
    }

    public void insetJqPlan(List<MachineProcess> machineProcesses,QueryCondition  queryCondition,Token token){
        //先遍历machineProcesses 存成一个map
        HashMap<String,MachineProcess> map =new HashMap<>();
        for (MachineProcess machineProcess : machineProcesses) {
            map.put(machineProcess.getName(),machineProcess);
        }
        //查询这个机型的部装信息
        Wrapper<JqbzSettings> jqbzSettingsWrapper=new EntityWrapper<>();
        jqbzSettingsWrapper.eq("type",queryCondition.getMacType());
        List<JqbzSettings> jqbzSettings = jqbzSettingsMapper.selectList(jqbzSettingsWrapper);
        for (JqbzSettings jqbzSetting : jqbzSettings) {
            MachineProcess machineProcess = map.get(jqbzSetting.getName());
            Integer nums = jqbzSetting.getNums();
            for (int i = 0; i <nums; i++) {
                JqPlan jqPlan =new JqPlan();
                jqPlan.setMakeNo(queryCondition.getMakeNo());
                jqPlan.setGid(jqbzSetting.getId());
                if (AdscmBaseUtils.getInstance().isNotEmpty(machineProcess,null)){
                    jqPlan.setStartTime(machineProcess.getStartTime());
                }
                jqPlan.setGxType("1");
                jqPlan.setState(0);
                jqPlan.setCreateTime(new Date());
                if (AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
                    jqPlan.setCreator(token.getUser().getId().toString());
                }
                jqPlanMapper.insert(jqPlan);
            }
        }
        //查询总装信息
        Wrapper<JqzzSetting> jqzzSettingWrapper=new EntityWrapper<>();
        jqzzSettingWrapper.eq("type",queryCondition.getMacType());
        List<JqzzSetting> jqzzSettings = jqzzSettingMapper.selectList(jqzzSettingWrapper);
        for (JqzzSetting jqzzSetting : jqzzSettings) {
            MachineProcess machineProcess = map.get(jqzzSetting.getName());
            Integer nums = jqzzSetting.getNums();
            for (int i = 0; i <nums; i++) {
                JqPlan jqPlan =new JqPlan();
                jqPlan.setMakeNo(queryCondition.getMakeNo());
                jqPlan.setGid(jqzzSetting.getId());
                if (AdscmBaseUtils.getInstance().isNotEmpty(machineProcess,null)){
                    jqPlan.setStartTime(machineProcess.getStartTime());
                }
                jqPlan.setGxType("2");
                jqPlan.setState(0);
                jqPlan.setCreateTime(new Date());
                if (AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
                    jqPlan.setCreator(token.getUser().getId().toString());
                }
                jqPlanMapper.insert(jqPlan);
            }
        }
        //测试
        Wrapper<JqcsSetting> jqcsSettingWrapper=new EntityWrapper<>();
        jqcsSettingWrapper.eq("type",queryCondition.getMacType());
        List<JqcsSetting> jqcsSettings = jqcsSettingMapper.selectList(jqcsSettingWrapper);
        for (JqcsSetting jqcsSetting : jqcsSettings) {
            MachineProcess machineProcess = map.get(jqcsSetting.getName());
            JqPlan jqPlan =new JqPlan();
            jqPlan.setMakeNo(queryCondition.getMakeNo());
            jqPlan.setGid(jqcsSetting.getId());
            if (AdscmBaseUtils.getInstance().isNotEmpty(machineProcess,null)){
                jqPlan.setStartTime(machineProcess.getStartTime());
            }
            jqPlan.setGxType("3");
            jqPlan.setState(0);
            jqPlan.setCreateTime(new Date());
            if (AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
                jqPlan.setCreator(token.getUser().getId().toString());
            }
            jqPlanMapper.insert(jqPlan);
        }
        //打包
        Wrapper<JqdbSettings> jqdbSettingsWrapper=new EntityWrapper<>();
        jqdbSettingsWrapper.eq("type",queryCondition.getMacType());
        List<JqdbSettings> jqdbSettings = jqdbSettingsMapper.selectList(jqdbSettingsWrapper);
        for (JqdbSettings jqdbSetting : jqdbSettings) {
            MachineProcess machineProcess = map.get(jqdbSetting.getName());
            JqPlan jqPlan =new JqPlan();
            jqPlan.setMakeNo(queryCondition.getMakeNo());
            jqPlan.setGid(jqdbSetting.getId());
            if (AdscmBaseUtils.getInstance().isNotEmpty(machineProcess,null)){
                jqPlan.setStartTime(machineProcess.getStartTime());
            }
            jqPlan.setGxType("4");
            jqPlan.setState(0);
            jqPlan.setCreateTime(new Date());
            if (AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
                jqPlan.setCreator(token.getUser().getId().toString());
            }
            jqPlanMapper.insert(jqPlan);
        }


    }
}
